package com.example.registerdiirectionlayoutyd203zlw2021606130.slice;

import com.example.registerdiirectionlayoutyd203zlw2021606130.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MainAbilitySlice extends AbilitySlice {
    Button login,Demo;
    TextField name,password;
    Context context;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Myid();
        context=getContext();
        login.setClickedListener(component -> main_login());
        Demo.setClickedListener(component -> main_Demo());
    }
    private void Myid() {
        login= (Button) findComponentById(ResourceTable.Id_login);
        Demo= (Button) findComponentById(ResourceTable.Id_Demo);
        name= (TextField) findComponentById(ResourceTable.Id_name);
        password= (TextField) findComponentById(ResourceTable.Id_password);
    }
    private void main_login() {
        String Name=name.getText().toString();
        String Password=password.getText().toString();
        DatabaseHelper databaseHelper=new DatabaseHelper(context);
        Preferences preferences=databaseHelper.getPreferences("pdb");
        String user=preferences.getString("name","");
        String PWD=preferences.getString("passwrod","");
        if (Name.isEmpty() | Password.isEmpty()){
            new ToastDialog(context).setText("您还没有输入！").show();
        }else if (Name.equals(user)&&Password.equals(PWD)){
            new ToastDialog(context).setText("登录成功").show();
        }else {
            new ToastDialog(context).setText("用户或者密码错误").show();
        }

    }
    private void main_Demo() {
        String main_Name=name.getText().toString();
        String main_Password=password.getText().toString();
        DatabaseHelper databaseHelper=new DatabaseHelper(context);
        Preferences preferences=databaseHelper.getPreferences("pdb");
        if (!main_Name.equals("") && !main_Password.equals("")){
            preferences.putString("name",main_Name);
            preferences.putString("passwrod",main_Password);
            preferences.flush();
            new ToastDialog(context).setText("注册成功").show();
        }else if (main_Name.equals("") | main_Password.equals("")){
            new ToastDialog(context).setText("您还没有输入！").show();
        }
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}